package cn.yangliu.nacos.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;

/**
 * The interface Json web token config.
 *
 * @author 问道于盲
 * @date 2019 -03-28
 */
@Configuration
public class JsonWebTokenConfig {

    /**
     * current class instance's member.
     * The Redis connection factory.
     */
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * Jwt token store jwt token store.
     *
     * @return the jwt token store
     */
    @Bean
    @Primary
    public JwtTokenStore jwtTokenStore() {
        RedisTokenStoreSerializationStrategy strategy = new JdkSerializationStrategy();
        return new JwtRedisTokenStore(jwtAccessTokenConverter(), redisConnectionFactory, strategy);
    }

    /**
     * Jwt access token converter jwt access token converter.
     *
     * @return the jwt access token converter
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey("qwertyuiop");
        return tokenConverter;
    }
}
